/*
 * @Author: 贺楠 1071780426@qq.com
 * @Date: 2024-04-25 09:57:11
 * @LastEditors: 贺楠 1071780426@qq.com
 * @LastEditTime: 2024-05-30 15:02:52
 * @FilePath: \smartConstructionSite\src\utils\common.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: 卡卡 1071780426@qq.com
 * @Date: 2023-03-21 12:16:15
 * @LastEditors: 贺楠 1071780426@qq.com
 * @LastEditTime: 2024-04-28 14:58:20
 * @FilePath: \mould\src\utils\date.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */






import {
    getUserCompanyTreeList
} from "@/api/request.js";


class API {
    getTree() {
        return new Promise((resolve, reject) => {
            try {
                getUserCompanyTreeList({
                        companyId: JSON.parse(sessionStorage.getItem("yjSmart-userInfo")).companyId,
                        userId: JSON.parse(sessionStorage.getItem("yjSmart-userInfo")).userId
                    })
                    .then((res) => {
                        if (res.errcode == 0) {
                            // console.log(res,this.tranListToTreeData(res.data.userCompanyTreeList));
                            var data=this.tranListToTreeData(res.data.userCompanyTreeList)
                            resolve(data)

                        }else {
                            this.$message.error('左侧树状图',res.msg);
                        }
                    })

            } catch (error) {
                console.log(error, '请求失败')
                reject(error)
            }
        })
    }


    tranListToTreeData(data) {
        //处理树状图数据
        let result = [];
        if (!Array.isArray(data)) {
            return result;
        }
        // 清除已存在的children 重新赋值
        data.forEach((item) => {
            delete item.children;
        });

        let map = {};
        data.forEach((item) => {
            map[item.fCompanyid] = item;
        });

        data.forEach((item) => {
            let parent = map[item.fParentid];
            if (parent) {
                (parent.children || (parent.children = [])).push(item);
            } else {
                result.push(item);
            }
        });
        return result;
    }

}
export default new API()

// 调用
// getAllPark(data => {
// console.log(data); // 成功拿到数据
// });